package com.gc.monitor.gctask;

import com.gc.gpsoo.beans.GpsooCarData;
import com.gc.gpsoo.beans.GpsooDevData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 从服务器上获取下来的缓存
 * Created by 瑱彬 on 2015/4/15.
 */
public class GpsooMonitorResult {
    private List<GpsooCarData> monitorResults;
    private List<GpsooDevData> devinfoResults;

    private Map<String, GpsooCarData> monitorMap;
    private Map<String, GpsooDevData> devinfoMap;
    private Map<String, MonitorStatusPojo> latestHistoriesStatus;


    public static GpsooMonitorResult gpsooMonitorResult;

    public static GpsooMonitorResult getInstance() {
        if (gpsooMonitorResult == null) {
            gpsooMonitorResult = new GpsooMonitorResult();
        }
        return gpsooMonitorResult;
    }

    public GpsooMonitorResult() {
        monitorResults = new ArrayList<>();
        devinfoResults = new ArrayList<>();

        monitorMap = new HashMap<>();
        devinfoMap = new HashMap<>();
    }

    public final List<GpsooCarData> getMonitorResults() {
        return monitorResults;
    }

    public final void setMonitorResults(List<GpsooCarData> monitorResults) {
        this.monitorResults = monitorResults;
        this.monitorMap = new HashMap<>();
        for (GpsooCarData gpsooCarData : this.monitorResults) {
            this.monitorMap.put(gpsooCarData.getImei(), gpsooCarData);
        }
    }

    public final List<GpsooDevData> getDevinfoResults() {
        return devinfoResults;
    }

    public final void setDevinfoResults(List<GpsooDevData> devinfoResults) {
        this.devinfoResults = devinfoResults;
        for (GpsooDevData gpsooDevData : this.devinfoResults) {
            this.devinfoMap.put(gpsooDevData.getImei(), gpsooDevData);
        }
    }

    public final Map<String, GpsooCarData> getMonitorMap() {
        return monitorMap;
    }

    public final Map<String, GpsooDevData> getDevinfoMap() {

        return devinfoMap;
    }

    public final Map<String, MonitorStatusPojo> getLatestHistoriesStatus() {
        if (latestHistoriesStatus == null) {
            initStatus();
        }
        return latestHistoriesStatus;
    }

    public final void setLatestHistoriesStatus(Map<String, MonitorStatusPojo> latestHistoriesStatus) {
        this.latestHistoriesStatus = latestHistoriesStatus;
    }

    public static MonitorStatusPojo updateWithGpsooCarDate(MonitorStatusPojo monitorStatusPojo, GpsooCarData gpsooCarData) {
        monitorStatusPojo.setTime(gpsooCarData.getServerTime());
        monitorStatusPojo.setBreakStatus(!(gpsooCarData.getSpeed() > 0));
        monitorStatusPojo.setPosLat(gpsooCarData.getLat() + "");//GpsooSDK里的是Float类型，问题并不是很大
        monitorStatusPojo.setPosLng(gpsooCarData.getLng() + "");
        return monitorStatusPojo;
    }

    private void initStatus() {
        latestHistoriesStatus = new HashMap<>();
        List<GpsooCarData> gpsooCarDatas = GpsooMonitorResult.getInstance().getMonitorResults();
        for (GpsooCarData gpsooCarData : gpsooCarDatas) {
            //如果latestHistoryStatus里没有，就新建一个
            MonitorStatusPojo statusPojo = new MonitorStatusPojo();
            statusPojo.setImei(gpsooCarData.getImei());
            statusPojo.setStopStatus(false);
            statusPojo.setStayStatus(false);

            statusPojo = updateWithGpsooCarDate(statusPojo, gpsooCarData);
            latestHistoriesStatus.put(statusPojo.getImei(), statusPojo);
        }

    }

    @Override
    public final String toString() {
        return "GpsooMonitorResult{" +
                "monitorResults=" + monitorResults +
                ", devinfoResults=" + devinfoResults +
                ", monitorMap=" + monitorMap +
                ", devinfoMap=" + devinfoMap +
                '}';
    }
}
